﻿using System;
using System.Collections.Generic;
using System.IO;

namespace DigitalQuran.Text
{
    /// <summary>
    /// Engine used to Simply Text for specific Text Mode
    /// </summary>
    public sealed class SimplificationEngine
    {
        /// <summary>
        /// Create new instance of SimplflicationEngine
        /// </summary>
        /// <param name="client">Client Instance</param>
        public SimplificationEngine()
        {            
            SimplificationRules = new Dictionary<string, SimplificationRule>();            
        }

        /// <summary>
        /// Gets the Underlaying Dictionary of Rules
        /// </summary>
        public Dictionary<string, SimplificationRule> SimplificationRules { get; private set; }

        internal void LoadRules()
        {
            string[] files = Directory.GetFiles(DigitalQuranDirectories.SimplificationRules + "/", "*.txt", SearchOption.TopDirectoryOnly);
            foreach (string file in files)
            {
                string textMode = Path.GetFileNameWithoutExtension(file);

                // remove some text modes from some Editions
                switch (Globals.Edition)
                {
                    case Edition.Lite:
                        {
                            if ((textMode == "Simplified29Shadda") || (textMode == "Simplified30") ||
                                (textMode == "Simplified31") || (textMode == "Simplified37"))
                                continue; // skip text mode                                        
                        }
                        break;
                    case Edition.Standard:
                    case Edition.Grammar:
                        {
                            if ((textMode == "Simplified30"))
                                continue; // skip text mode                                        
                        }
                        break;
                }

                Client.Instance.TextModes.Add(textMode);
                SimplificationRules.Add(textMode, SimplificationRule.LoadFromFile(file));
            }
        }

        /// <summary>
        /// Simplifies the text
        /// </summary>
        /// <param name="text">text to simplify</param>
        /// <param name="toTextMode">name of text mode for which to simplify</param>
        /// <returns>Simplified text according to the rules</returns>
        public string SimplifyText(string text, string toTextMode)
        {
            SimplificationRule rule;
            if (SimplificationRules.TryGetValue(toTextMode, out rule))
            {
                return rule.SimplifyText(text);
            }
            
            return String.Empty;
        }
    }
}